<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>变量作用域</title>
</head>

<body>
    <script>
        var num = 10

        function test() {
            console.log('test num 1', num) // undefined
            // 变量声明提前
            var num = 15
            console.log('test num 2', num) // 15
        }

        console.log('全局变量num', num) // 10
        test()
    </script>
    <script>
        // 下面的代码会出错
        var num = 10

        function test() {
            // Uncaught ReferenceError: Cannot access 'num' before initialization
            // console.log('test num 1', num)

            let num = 15
            console.log('test num 2', num) // 15
        }

        console.log('全局变量num', num) // 10
        test()
    </script>

</body>

</html>